using System;
using System.Text;
using System.IO;

namespace ThalesHsmComms
{
	public class TranslatePinLmk2ZpkMsg : WSMMsg
	{
		private String destPin;
		public String CheckDigits{private set; get;}
		private String zpk;
		public String Pin{private set; get;}
		private String pan;

		public TranslatePinLmk2ZpkMsg(String zpk, String pin, String pan)
			:base("JG")
		{			
			this.zpk = "U" + zpk;
			this.Pin = pin;
	        this.pan = pan;
		}

	override protected void Encode() 
	{

		StringBuilder data = new StringBuilder();
		
		try 
		{
		
			// pack command code
			data.Append(this.commandCode);
		
			// pack zpk
			data.Append(this.zpk);
					
			// set pin block format
			data.Append("01");
            
            // set 12 right-most digits of account number excluding check digit
            data.Append(this.pan.Substring(pan.Length - 13, 11));
		
			// pack pin to translate
			data.Append(this.Pin);
            
			this.requestData = ASCIIEncoding.ASCII.GetBytes(data.ToString());
		
		} catch (IOException ex) {
			Console.WriteLine(ex);
		}
	}


	override protected void Decode() 
	{
		// get pin under lmk
		//getBytes(1);
			this.destPin = this.GetNextBytesFromResponse(16);
		
		
	}


	override public String ToString() {
	
		StringBuilder str = new StringBuilder();
		
		str.Append("Request\n");
		str.Append("=======\n");
		str.Append("Raw data: " + this.requestData + "\n");
		str.Append("Command code: " + this.commandCode + "\n");
		str.Append("ZPK: " + this.zpk + "\n");
		str.Append("PIN: " + this.Pin + "\n");
        str.Append("PAN: " + this.pan + "\n");
		
		str.Append("\nResponse\n");
		str.Append("========\n");
		str.Append("Raw data: " + this.responseData + "\n");
		str.Append("Response code: " + this.responseCode + "\n");
		str.Append("Error code: " + this.errorCode + "\n");
		str.Append("Destination PIN: " + this.destPin + "\n");
				
		return str.ToString();
	}
	
	}
}

